//
//  LightSourceControllerManager.swift
//  PeerTalkPlaygroundWithSPM
//
//  Created by Cosmetic Cygia on 2024/8/22.
//

import Foundation


protocol LightSourceController {
    
    var ipAddress: String? { get }
    var port: Int? { get }
    var timeout: Double? { get set }
    var tcpManager: TcpManager? { get }
    
    init(ipAddress: String, port: Int, timeout: Double)
    
}

class OptLightSourceController: LightSourceController {
    
    var tcpManager: TcpManager?
    
    var ipAddress: String?
    
    var port: Int?
    
    var timeout: Double?
    
    required init(ipAddress: String, port: Int, timeout: Double = 0) {
        self.ipAddress = ipAddress
        self.port = port
        self.tcpManager = TcpManager(ipAddress: ipAddress, port: port, timeout: timeout)
        
        self.tcpManager?.deprecateStreamRead()
    }
}

class OptFlashLightSourceController: OptLightSourceController {
    
    func setPulseWidth(channel: UInt8, pulseWidth: Int, escape: ((_ isSuccess: Bool) -> Void)?) {
        
        var data: [UInt8] = [0xff, 0x02]
        
        data.append(channel & 0xff)
        data.append(UInt8((pulseWidth / 256) & 0xff))
        data.append(UInt8((pulseWidth % 256) & 0xff))
        data.append(XorCheckSum(data: data))
        
        tcpManager?.sendUInt8(dataToSend: data) {isSuccess,response in 
            let _: Bool = (isSuccess && (response.count == 2) && (response[0] == data[3]) && (response[1] == data[4]))
            escape?(isSuccess)
        }
    }
    
    
    func trigger(channel: UInt8, escape: ((_ isSuccess: Bool) -> Void)?) {
        
        var data: [UInt8] = [0xff, 0x05]
        
        data.append(channel & 0xff)
        data.append(0x00)
        data.append(0x01)
        data.append(XorCheckSum(data: data))
        
        tcpManager?.sendUInt8(dataToSend: data) {isSuccess,response in
            let _: Bool = (isSuccess && (response.count == 2) && (response[0] == data[3]) && (response[1] == data[4]))
            escape?(isSuccess)
        }
    }
}


class OptPermanentLightSourceController: OptLightSourceController {
    
    func setLedValue(channel: UInt8, value: Int, escape: ((_ isSuccess: Bool) -> Void)?) {
        
        var data: [UInt8] = [0xff, 0x01]
        
        data.append(channel & 0xff)
        data.append(0x00)
        data.append(UInt8((value % 256) & 0xff))
        data.append(XorCheckSum(data: data))
        
        tcpManager?.sendUInt8(dataToSend: data) {isSuccess,response in
            let _: Bool = (isSuccess && (response.count == 2) && (response[0] == data[3]) && (response[1] == data[4]))
            escape?(isSuccess)
        }
    }
    
    func setLed(channel: UInt8, status: Bool, escape: ((_ isSuccess: Bool) -> Void)?) {
        var data: [UInt8] = [0xff, 0x05]
        
        data.append(channel & 0xff)
        data.append(0x00)
        data.append(status ? 0x01 : 0x00)
        data.append(XorCheckSum(data: data))
        
        tcpManager?.sendUInt8(dataToSend: data) {isSuccess,response in
            let _: Bool = (isSuccess && (response.count == 2) && (response[0] == data[3]) && (response[1] == data[4]))
            escape?(isSuccess)
        }
    }
}
